package com.mystudy.management.usermanagement.service;


import com.mystudy.management.usermanagement.dao.mappers.RequestTraceMapper;
import com.mystudy.management.usermanagement.dao.mappers.VisitTraceMapper;
import com.mystudy.management.usermanagement.dao.pojo.RequestTrace;
import com.mystudy.management.usermanagement.dao.pojo.RequestTraceExample;
import com.mystudy.management.usermanagement.dao.pojo.VisitTrace;
import com.mystudy.management.usermanagement.dao.pojo.VisitTraceExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(
        isolation = Isolation.REPEATABLE_READ
)
public class index2_3ServiceImpl implements index2_3Service{

    @Autowired
    private RequestTraceMapper requestTraceMapper;

    @Autowired
    private VisitTraceMapper visitTraceMapper;

    //获取所有访问记录
    @Override
    public Map<String, Integer> getAllRequestTrace() {
        List<RequestTrace> requestTraces = requestTraceMapper.selectByExample(null);
        Map<String,Integer> requestFloatMap = new HashMap<>();//流量映射表，存放最终返回的数据
        Map<String,Integer> requestTracesMap = new HashMap<>();//记录映射表，存放记录的
        //通过选择查找找出服务器有访问记录的日期区间(无数据时，横坐标默认为一个月的日期长度)
        Calendar cStart = Calendar.getInstance();
        Date endDate = new Date();
        cStart.setTime(endDate);
        cStart.add(Calendar.MONTH,- 1);
        Date startDate = cStart.getTime();
        if(requestTraces.size() != 0){
             startDate = requestTraces.get(0).getTraceDate();
             endDate = startDate;
            for (int i = 0;i<requestTraces.size();i++){
                //对记录进行统计
                String date1 = new SimpleDateFormat("yyyy-MM-dd").format(requestTraces.get(i).getTraceDate());
                if(!requestTracesMap.containsKey(date1)){
                    requestTracesMap.put(date1,1);
                    for(int j = i+1;j<requestTraces.size();j++){
                        String date2 = new SimpleDateFormat("yyyy-MM-dd").format(requestTraces.get(j).getTraceDate());
                        if (date1.equals(date2)){
                            requestTracesMap.put(date2,requestTracesMap.get(date2)+1);

                        }
                    }
                }

                //查找两端日期
                if(requestTraces.get(i).getTraceDate().before(startDate)){
                    startDate = requestTraces.get(i).getTraceDate();
                }

                if(requestTraces.get(i).getTraceDate().after(endDate)){
                    endDate = requestTraces.get(i).getTraceDate();
                }
            }
        }


        //生成有访问记录日期区间的所有日期,并加入有效记录
        Date indexDate = startDate;
        Calendar cEnd = Calendar.getInstance();
        cEnd.setTime(endDate);
        cEnd.add(Calendar.DATE,+ 1);
        endDate = cEnd.getTime();//截至日期比实际日期多一天，防止漏记录
        while(!indexDate.after(endDate)){
            String indexDateString = new SimpleDateFormat("yyyy-MM-dd").format(indexDate);
            requestFloatMap.put(indexDateString,0);
            if(requestTracesMap.containsKey(indexDateString)){
                requestFloatMap.put(indexDateString,requestTracesMap.get(indexDateString));
            }
            Calendar cIndex = Calendar.getInstance();
            cIndex.setTime(indexDate);
            cIndex.add(Calendar.DATE,+1);
            indexDate = cIndex.getTime();
        }
        System.out.println(requestTracesMap);
        System.out.println(requestFloatMap);


        return requestFloatMap;
    }

    //获取近一周的请求记录
    @Override
    public Map<String, Integer> getLastWeekRequestTrace() throws ParseException {
       Date nowDate = new Date();
       Calendar c = Calendar.getInstance();
       c.setTime(nowDate);
       c.add(Calendar.DATE,- 7);
       Date lastWeekDate = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()));
       RequestTraceExample example = new RequestTraceExample();
       example.createCriteria().andTraceDateBetween(lastWeekDate,nowDate);
        List<RequestTrace> requestTraces = requestTraceMapper.selectByExample(example);
        Map<String,Integer> requestFloatMap = new HashMap<>();//流量映射表，存放最终返回的数据
        Map<String,Integer> requestTracesMap = new HashMap<>();//记录映射表，存放记录的
        //通过选择查找找出服务器有访问记录的日期区间
        if(requestTraces.size() != 0){
            for (int i = 0;i<=requestTraces.size();i++){
                //对记录进行统计
                String date1 = new SimpleDateFormat("yyyy-MM-dd").format(requestTraces.get(i).getTraceDate());
                if(!requestTracesMap.containsKey(date1)){
                    requestTracesMap.put(date1,1);
                    for(int j = i+1;j<requestTraces.size();j++){
                        String date2 = new SimpleDateFormat("yyyy-MM-dd").format(requestTraces.get(j).getTraceDate());
                        if (date1.equals(date2)){
                            requestTracesMap.put(date2,requestTracesMap.get(date2)+1);

                        }
                    }
                }
            }
        }

        //生成有访问记录日期区间的所有日期,并加入有效记录
        Date indexDate = lastWeekDate;
        while(!indexDate.after(nowDate)){
            String indexDateString = new SimpleDateFormat("yyyy-MM-dd").format(indexDate);
            requestFloatMap.put(indexDateString,0);
            if(requestTracesMap.containsKey(indexDateString)){
                requestFloatMap.put(indexDateString,requestTracesMap.get(indexDateString));
            }
            Calendar cIndex = Calendar.getInstance();
            cIndex.setTime(indexDate);
            cIndex.add(Calendar.DATE,+1);
            indexDate = cIndex.getTime();
        }
        System.out.println(requestTracesMap);
        System.out.println(requestFloatMap);


        return requestFloatMap;
    }

    //获取近一个月的请求记录
    @Override
    public Map<String, Integer> getLastMonthRequestTrace() throws ParseException {
        Date nowDate = new Date();
        Calendar cStart = Calendar.getInstance();
        cStart.setTime(nowDate);
        cStart.add(Calendar.MONTH,- 1);
        Date lastMonthDate = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(cStart.getTime()));
        RequestTraceExample example = new RequestTraceExample();
        example.createCriteria().andTraceDateBetween(lastMonthDate,nowDate);
        List<RequestTrace> requestTraces = requestTraceMapper.selectByExample(example);
        Map<String,Integer> requestFloatMap = new HashMap<>();//流量映射表，存放最终返回的数据
        Map<String,Integer> requestTracesMap = new HashMap<>();//记录映射表，存放记录的
        //通过选择查找找出服务器有访问记录的日期区间(先设置默认起止日期)
        if(requestTraces.size()!=0){
            for (int i = 0;i<requestTraces.size();i++){
                //对记录进行统计
                String date1 = new SimpleDateFormat("yyyy-MM-dd").format(requestTraces.get(i).getTraceDate());
                if(!requestTracesMap.containsKey(date1)){
                    requestTracesMap.put(date1,1);
                    for(int j = i+1;j<requestTraces.size();j++){
                        String date2 = new SimpleDateFormat("yyyy-MM-dd").format(requestTraces.get(j).getTraceDate());
                        if (date1.equals(date2)){
                            requestTracesMap.put(date2,requestTracesMap.get(date2)+1);

                        }
                    }
                }

            }
        }

        //生成有访问记录日期区间的所有日期,并加入有效记录
        Date indexDate = lastMonthDate;
        while(!indexDate.after(nowDate)){
            String indexDateString = new SimpleDateFormat("yyyy-MM-dd").format(indexDate);
            requestFloatMap.put(indexDateString,0);
            if(requestTracesMap.containsKey(indexDateString)){
                requestFloatMap.put(indexDateString,requestTracesMap.get(indexDateString));
            }
            Calendar cIndex = Calendar.getInstance();
            cIndex.setTime(indexDate);
            cIndex.add(Calendar.DATE,+1);
            indexDate = cIndex.getTime();
        }
        System.out.println(requestTracesMap);
        System.out.println(requestFloatMap);
        return requestFloatMap;

    }

    //根据日期区间获取
    @Override
    public Map<String, Integer> getRequestTraceByCustomDateRange(Date[] dates) throws ParseException {
        RequestTraceExample example = new RequestTraceExample();
        example.createCriteria().andTraceDateBetween(dates[0], dates[1]);
        List<RequestTrace> requestTraces = requestTraceMapper.selectByExample(example);
        Map<String, Integer> requestFloatMap = new HashMap<>();//流量映射表，存放最终返回的数据
        Map<String, Integer> requestTracesMap = new HashMap<>();//记录映射表，存放记录的
        //通过选择查找找出服务器有访问记录的日期区间
        Date startDate = dates[0];
        Date endDate = dates[1];
        if(requestTraces.size() != 0){
             startDate = requestTraces.get(0).getTraceDate();
            endDate = startDate;
            for (int i = 0; i < requestTraces.size(); i++) {
                //对记录进行统计
                String date1 = new SimpleDateFormat("yyyy-MM-dd").format(requestTraces.get(i).getTraceDate());
                if (!requestTracesMap.containsKey(date1)) {
                    requestTracesMap.put(date1, 1);
                    for (int j = i + 1; j < requestTraces.size(); j++) {
                        String date2 = new SimpleDateFormat("yyyy-MM-dd").format(requestTraces.get(j).getTraceDate());
                        if (date1.equals(date2)) {
                            requestTracesMap.put(date2, requestTracesMap.get(date2) + 1);

                        }
                    }
                }

                //查找两端日期
                if (requestTraces.get(i).getTraceDate().before(startDate)) {
                    startDate = requestTraces.get(i).getTraceDate();
                }

                if (requestTraces.get(i).getTraceDate().after(endDate)) {
                    endDate = requestTraces.get(i).getTraceDate();
                }
            }
        }

        //生成有访问记录日期区间的所有日期,并加入有效记录
        Date indexDate = startDate;
        while (!indexDate.after(endDate)) {
            String indexDateString = new SimpleDateFormat("yyyy-MM-dd").format(indexDate);
            requestFloatMap.put(indexDateString, 0);
            if (requestTracesMap.containsKey(indexDateString)) {
                requestFloatMap.put(indexDateString, requestTracesMap.get(indexDateString));
            }
            Calendar cIndex = Calendar.getInstance();
            cIndex.setTime(indexDate);
            cIndex.add(Calendar.DATE, +1);
            indexDate = cIndex.getTime();
        }
        System.out.println(requestTracesMap);
        System.out.println(requestFloatMap);
        return requestFloatMap;
    }

    @Override
    public Map<String, Integer> getAllVisitTrace() {
        List<VisitTrace> visitTraces = visitTraceMapper.selectByExample(null);
        Map<String,Integer> visitFloatMap = new HashMap<>();//流量映射表，存放最终返回的数据
        Map<String,Integer> visitTracesMap = new HashMap<>();//记录映射表，存放记录的
        //通过选择查找找出服务器有访问记录的日期区间(无数据时，横坐标默认为一个月的日期长度)
        Calendar cStart = Calendar.getInstance();
        Date endDate = new Date();
        cStart.setTime(endDate);
        cStart.add(Calendar.MONTH,- 1);
        Date startDate = cStart.getTime();
        if(visitTraces.size() != 0){
            startDate = visitTraces.get(0).getVisitDate();
            endDate = startDate;
            for (int i = 0;i<visitTraces.size();i++){
                //对记录进行统计
                String date1 = new SimpleDateFormat("yyyy-MM-dd").format(visitTraces.get(i).getVisitDate());
                if(!visitTracesMap.containsKey(date1)){
                    visitTracesMap.put(date1,1);
                    for(int j = i+1;j<visitTraces.size();j++){
                        String date2 = new SimpleDateFormat("yyyy-MM-dd").format(visitTraces.get(j).getVisitDate());
                        if (date1.equals(date2)){
                            visitTracesMap.put(date2,visitTracesMap.get(date2)+1);

                        }
                    }
                }

                //查找两端日期
                if(visitTraces.get(i).getVisitDate().before(startDate)){
                    startDate = visitTraces.get(i).getVisitDate();
                }

                if(visitTraces.get(i).getVisitDate().after(endDate)){
                    endDate = visitTraces.get(i).getVisitDate();
                }
            }
        }


        //生成有访问记录日期区间的所有日期,并加入有效记录
        Date indexDate = startDate;
        Calendar cEnd = Calendar.getInstance();
        cEnd.setTime(endDate);
        cEnd.add(Calendar.DATE,+ 1);
        endDate = cEnd.getTime();//截至日期比实际日期多一天，防止漏记录
        while(!indexDate.after(endDate)){
            String indexDateString = new SimpleDateFormat("yyyy-MM-dd").format(indexDate);
            visitFloatMap.put(indexDateString,0);
            if(visitTracesMap.containsKey(indexDateString)){
                visitFloatMap.put(indexDateString,visitTracesMap.get(indexDateString));
            }
            Calendar cIndex = Calendar.getInstance();
            cIndex.setTime(indexDate);
            cIndex.add(Calendar.DATE,+1);
            indexDate = cIndex.getTime();
        }
        System.out.println(visitTracesMap);
        System.out.println(visitFloatMap);


        return visitFloatMap;
    }

    @Override
    public Map<String, Integer> getLastWeekVisitTrace() throws ParseException {
        Date nowDate = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(nowDate);
        c.add(Calendar.DATE,- 7);
        Date lastWeekDate = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()));
        VisitTraceExample example = new VisitTraceExample();
        example.createCriteria().andVisitDateBetween(lastWeekDate,nowDate);
        List<VisitTrace> visitTraces = visitTraceMapper.selectByExample(example);
        Map<String,Integer> visitFloatMap = new HashMap<>();//流量映射表，存放最终返回的数据
        Map<String,Integer> visitTracesMap = new HashMap<>();//记录映射表，存放记录的
        //通过选择查找找出服务器有访问记录的日期区间
        if(visitTraces.size() != 0){
            for (int i = 0;i<=visitTraces.size();i++){
                //对记录进行统计
                String date1 = new SimpleDateFormat("yyyy-MM-dd").format(visitTraces.get(i).getVisitDate());
                if(!visitTracesMap.containsKey(date1)){
                    visitTracesMap.put(date1,1);
                    for(int j = i+1;j<visitTraces.size();j++){
                        String date2 = new SimpleDateFormat("yyyy-MM-dd").format(visitTraces.get(j).getVisitDate());
                        if (date1.equals(date2)){
                            visitTracesMap.put(date2,visitTracesMap.get(date2)+1);

                        }
                    }
                }
            }
        }

        //生成有访问记录日期区间的所有日期,并加入有效记录
        Date indexDate = lastWeekDate;
        while(!indexDate.after(nowDate)){
            String indexDateString = new SimpleDateFormat("yyyy-MM-dd").format(indexDate);
            visitFloatMap.put(indexDateString,0);
            if(visitTracesMap.containsKey(indexDateString)){
                visitFloatMap.put(indexDateString,visitTracesMap.get(indexDateString));
            }
            Calendar cIndex = Calendar.getInstance();
            cIndex.setTime(indexDate);
            cIndex.add(Calendar.DATE,+1);
            indexDate = cIndex.getTime();
        }
        System.out.println(visitTracesMap);
        System.out.println(visitFloatMap);


        return visitFloatMap;
    }

    @Override
    public Map<String, Integer> getLastMonthVisitTrace() throws ParseException {
        Date nowDate = new Date();
        Calendar cStart = Calendar.getInstance();
        cStart.setTime(nowDate);
        cStart.add(Calendar.MONTH,- 1);
        Date lastMonthDate = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(cStart.getTime()));
        VisitTraceExample example = new VisitTraceExample();
        example.createCriteria().andVisitDateBetween(lastMonthDate,nowDate);
        List<VisitTrace> visitTraces = visitTraceMapper.selectByExample(example);
        Map<String,Integer> visitFloatMap = new HashMap<>();//流量映射表，存放最终返回的数据
        Map<String,Integer> visitTracesMap = new HashMap<>();//记录映射表，存放记录的
        //通过选择查找找出服务器有访问记录的日期区间(先设置默认起止日期)
        if(visitTraces.size()!=0){
            for (int i = 0;i<visitTraces.size();i++){
                //对记录进行统计
                String date1 = new SimpleDateFormat("yyyy-MM-dd").format(visitTraces.get(i).getVisitDate());
                if(!visitTracesMap.containsKey(date1)){
                    visitTracesMap.put(date1,1);
                    for(int j = i+1;j<visitTraces.size();j++){
                        String date2 = new SimpleDateFormat("yyyy-MM-dd").format(visitTraces.get(j).getVisitDate());
                        if (date1.equals(date2)){
                            visitTracesMap.put(date2,visitTracesMap.get(date2)+1);

                        }
                    }
                }

            }
        }

        //生成有访问记录日期区间的所有日期,并加入有效记录
        Date indexDate = lastMonthDate;
        while(!indexDate.after(nowDate)){
            String indexDateString = new SimpleDateFormat("yyyy-MM-dd").format(indexDate);
            visitFloatMap.put(indexDateString,0);
            if(visitTracesMap.containsKey(indexDateString)){
                visitFloatMap.put(indexDateString,visitTracesMap.get(indexDateString));
            }
            Calendar cIndex = Calendar.getInstance();
            cIndex.setTime(indexDate);
            cIndex.add(Calendar.DATE,+1);
            indexDate = cIndex.getTime();
        }
        System.out.println(visitTracesMap);
        System.out.println(visitFloatMap);
        return visitFloatMap;

    }

    @Override
    public Map<String, Integer> getVisitTraceByCustomDateRange(Date[] dates) throws ParseException {
        VisitTraceExample example = new VisitTraceExample();
        example.createCriteria().andVisitDateBetween(dates[0], dates[1]);
        List<VisitTrace> visitTraces = visitTraceMapper.selectByExample(example);
        Map<String, Integer> visitFloatMap = new HashMap<>();//流量映射表，存放最终返回的数据
        Map<String, Integer> visitTracesMap = new HashMap<>();//记录映射表，存放记录的
        //通过选择查找找出服务器有访问记录的日期区间
        Date startDate = dates[0];
        Date endDate = dates[1];
        if(visitTraces.size() != 0){
            startDate = visitTraces.get(0).getVisitDate();
            endDate = startDate;
            for (int i = 0; i < visitTraces.size(); i++) {
                //对记录进行统计
                String date1 = new SimpleDateFormat("yyyy-MM-dd").format(visitTraces.get(i).getVisitDate());
                if (!visitTracesMap.containsKey(date1)) {
                    visitTracesMap.put(date1, 1);
                    for (int j = i + 1; j < visitTraces.size(); j++) {
                        String date2 = new SimpleDateFormat("yyyy-MM-dd").format(visitTraces.get(j).getVisitDate());
                        if (date1.equals(date2)) {
                            visitTracesMap.put(date2, visitTracesMap.get(date2) + 1);

                        }
                    }
                }

                //查找两端日期
                if (visitTraces.get(i).getVisitDate().before(startDate)) {
                    startDate = visitTraces.get(i).getVisitDate();
                }

                if (visitTraces.get(i).getVisitDate().after(endDate)) {
                    endDate = visitTraces.get(i).getVisitDate();
                }
            }
        }

        //生成有访问记录日期区间的所有日期,并加入有效记录
        Date indexDate = startDate;
        while (!indexDate.after(endDate)) {
            String indexDateString = new SimpleDateFormat("yyyy-MM-dd").format(indexDate);
            visitFloatMap.put(indexDateString, 0);
            if (visitTracesMap.containsKey(indexDateString)) {
                visitFloatMap.put(indexDateString, visitTracesMap.get(indexDateString));
            }
            Calendar cIndex = Calendar.getInstance();
            cIndex.setTime(indexDate);
            cIndex.add(Calendar.DATE, +1);
            indexDate = cIndex.getTime();
        }
        System.out.println(visitTracesMap);
        System.out.println(visitFloatMap);
        return visitFloatMap;
    }
}
